/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 *
 */
package rad.framework.jsf;

import java.security.Principal;
import java.util.Map;

import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.jboss.seam.core.Expressions;
import org.jboss.seam.core.Expressions.ValueExpression;
import org.jboss.seam.web.ServletContexts;

/**
 * Utilities for working with Java Server Faces.
 * 
 * @author boudyacho
 * @version 1.0
 * 
 */
public class JsfUtil {

	public static String getApplicationName(FacesContext ctx) {
		if (ctx == null) {
			return "";
		}
		ExternalContext ectx = ctx.getExternalContext();
		if (ectx == null) {
			return "";
		}
		return ctx.getExternalContext().getRequestContextPath()
				.replace("/", "");
	}

	public static String getUrlPrefix(FacesContext ctx) {
		if (ctx == null) {
			return "";
		}
		ExternalContext ectx = ctx.getExternalContext();
		if (ectx == null) {
			return "";
		}
		Object r = ectx.getRequest();
		if (r instanceof ServletRequest) {
			return getUrlPrefix((ServletRequest) r);
		}
		return null;
	}

	public static String getUrlPrefix(ServletRequest request) {
		String protocol = "http";
		String host = "localhost";
		int port = 8080;
		if (request.isSecure()) {
			protocol = "https";
		}
		host = request.getServerName();
		port = request.getServerPort();
		return protocol + "://" + host + ":" + port;
	}

	public static String getViewId() {
		FacesContext fc = FacesContext.getCurrentInstance();
		if (fc == null) {
			return "";
		} else {
			UIViewRoot vr = fc.getViewRoot();
			return vr.getViewId();
		}
	}
	
	public static Principal getUserPrincipal() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		if (facesContext != null) {
			return facesContext.getExternalContext().getUserPrincipal();
		}

		ServletRequest servletRequest = ServletContexts.instance().getRequest();
		if (servletRequest != null) {
			return ((HttpServletRequest) servletRequest).getUserPrincipal();
		}

		return null;
	}
	
	public static Map<String, Object> getRequestMap() {
		return FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
	}

	public static ValueExpression<?> getExpression(final String exp) {
		return Expressions.instance().createValueExpression(exp);
	}

	public static <T> ValueExpression<T> getExpression(final String exp, final Class<T> type) {
		return Expressions.instance().createValueExpression(exp, type);
	}

	public static boolean getValue(ValueExpression<Boolean> expression,
			boolean defaultValue) {
		if (expression == null) {
			return defaultValue;
		} else {
			Boolean value = expression.getValue();
			if (value == null) {
				return true;
			} else {
				return value.booleanValue();
			}
		}
	}
}
